<?php
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/utilities/fpdf/fpdf.php';
	
	$ciRif = $_POST['ciRif'];
	$clientName = $_POST['clientName'];
	$secondName = $_POST['secondName'];
	$firstLastName = $_POST['firstLastName'];
	$secondLastName = $_POST['secondLastName'];
	$emailAddress = $_POST['emailAddress'];
	$address = $_POST['address'];
	$addressExplode = explode(", ", $address);
	$address = $addressExplode[0] . ', ' . $addressExplode[1];
	$billDateMembers = explode('-', $_POST['billDate']);
	$billDate = $billDateMembers[2] . '-' . $billDateMembers[1] . '-' . 
		substr($billDateMembers[0], 2);
	$billTax = $_POST['tax'];
	$billNumber = $_POST['number'];
	$billControlNumber = $_POST['controlNumber'];
	$products = $_POST['products'];
	
	function getProductsDetails($products) {
		$productsDetails = explode("\n", $products);
		for ($i = 0; $i < count($productsDetails); $i++) {
			$productDetails = explode(", ", $productsDetails[$i]);
			$billProducts['line'][$i] = $i + 1;
			$billProducts['description'][$i] = $productDetails[1];
			
			$productUnits = explode(' ', $productDetails[2]);
			$billProducts['units'][$i] = substr($productUnits[1], 0, 1);
			$billProducts['quantity'][$i] = $productUnits[0];
			
			$productCost = explode(' ', $productDetails[3]);
			$billProducts['unit_cost'][$i] = $productCost[1];
			
			$totalCost = explode(' ', $productDetails[4]);
			$billProducts['total_cost'][$i] = $totalCost[1];
		}
		
		return $billProducts;
	}
	
	$pdf = new FPDF('P', 'cm', 'Letter');
	$pdf -> AddPage();
	$pdf -> SetFont('Arial', 'B', 9);
	
	/**
	 * Creating bill header
	 */
	$pdf -> Cell(13);
	$pdf -> Cell(4.5, 0.7, utf8_decode('FACTURA N° ' . $billNumber), 1, 2, 'L');
	$pdf -> Cell(4.5, 0.7, 'Fecha ' . $billDate, 1, 1, 'L');
	
	/**
	 * Creating customer and address header
	 */
	$pdf -> Cell(13, 0.7, utf8_decode('Señores: Ductobarra'), 1, 1, 'L');
	$pdf -> Cell(17.5, 0.7, utf8_decode('Dirección: '), 1, 1, 'L');
	
	/**
	 * Creating delivery notes header
	 */
	$pdf -> Cell(3, 0.5, 'Notas de entrega', 'LTR', 0, 'C');
	$pdf -> Cell(3, 0.5, 'Orden de', 'LTR', 0, 'C');
	$pdf -> Cell(3, 0.5, 'Condiciones de', 'LTR', 0, 'C');
	$pdf -> Cell(3, 0.5, 'Presupuesto', 'LTR', 0, 'C');
	$pdf -> Cell(5.5, 0.5, 'Sitio de entrega', 'LTR', 1, 'C');
	$pdf -> Cell(3, 0.5, '', 'LBR', 0, 'C');
	$pdf -> Cell(3, 0.5, 'compra', 'LBR', 0, 'C');
	$pdf -> Cell(3, 0.5, 'pago', 'LBR', 0, 'C');
	$pdf -> Cell(3, 0.5, '', 'LBR', 0, 'C');
	$pdf -> Cell(5.5, 0.5, '', 'LBR', 1, 'C');
	$pdf -> SetFont('Arial', '', 9);
	$pdf -> Cell(3, 1, '', 1, 0, 'C');
	$pdf -> Cell(3, 1, '', 1, 0, 'C');
	$pdf -> Cell(3, 1, '', 1, 0, 'C');
	$pdf -> Cell(3, 1, '', 1, 0, 'C');
	$pdf -> Cell(5.5, 1, $address, 1, 1, 'C');
	
	/**
	 * Creating products header
	 */
	$pdf -> SetFont('Arial', 'B', 9);
	$pdf -> Cell(1.7, 0.5, 'Cantidad', 'LTR', 0, 'C');
	$pdf -> Cell(1.7, 0.5, 'Unidad', 'LTR', 0, 'C');
	$pdf -> Cell(9.5, 0.5, utf8_decode('Descripción'), 'LTR', 0, 'C');
	$pdf -> Cell(2.1, 0.5, 'Precio', 'LTR', 0, 'C');
	$pdf -> Cell(2.5, 0.5, 'Total Bs', 'LTR', 1, 'C');
	$pdf -> Cell(1.7, 0.5, '', 'LBR', 0, 'C');
	$pdf -> Cell(1.7, 0.5, '', 'LBR', 0, 'C');
	$pdf -> Cell(9.5, 0.5, '', 'LBR', 0, 'C');
	$pdf -> Cell(2.1, 0.5, 'unitario', 'LBR', 0, 'C');
	$pdf -> Cell(2.5, 0.5, '', 'LBR', 1, 'C');
	
	$billTotal = 0;
	$billProducts = getProductsDetails($products);
	$pdf -> SetFont('Arial', '', 9);
	for ($i = 0; $i < count($billProducts['quantity']); $i++) {
		$pdf -> Cell(1.7, 0.5, $billProducts['quantity'][$i], 'LR', 0, 'C');
		$pdf -> Cell(1.7, 0.5, $billProducts['units'][$i], 'LR', 0, 'C');
		$pdf -> Cell(9.5, 0.5, $billProducts['description'][$i], 'LR', 0, 'L');
		$pdf -> Cell(2.1, 0.5, $billProducts['unit_cost'][$i], 'LR', 0, 'C');
		$pdf -> Cell(2.5, 0.5, $billProducts['total_cost'][$i], 'LR', 1, 'C');
		$billTotal += $billProducts['total_cost'][$i];
	}
	
	for ($i = 0; $i < 10; $i++) {
		$pdf -> Cell(1.7, 0.5, '', 'LR', 0, 'C');
		$pdf -> Cell(1.7, 0.5, '', 'LR', 0, 'C');
		$pdf -> Cell(9.5, 0.5, '', 'LR', 0, 'L');
		$pdf -> Cell(2.1, 0.5, '', 'LR', 0, 'C');
		$pdf -> Cell(2.5, 0.5, '', 'LR', 1, 'C');
	}
	
	/**
	 * Creating footer
	 */
	$pdf -> SetFont('Arial', 'B', 9);
	$pdf -> Cell(3.5, 0.4, 'Condiciones Generales', 'LT', 0, 'L');
	$pdf -> SetFont('Arial', '', 9);
	$pdf -> Cell(9.4, 0.4, 
		utf8_decode(': Entregas parciales son permitidas y se facturan en proporción'), 
		'TR', 0, 'L');
	$pdf -> SetFont('Arial', 'B', 9);
	$pdf -> Cell(2.1, 0.4, 'Subtotal', 1, 0, 'C');	
	$pdf -> SetFont('Arial', '', 9);
	if (count(explode('.', $billTotal)) != 2)
		$pdf -> Cell(2.5, 0.4, $billTotal . '.00', 1, 1, 'C');
	else
		$pdf -> Cell(2.5, 0.4, $billTotal, 1, 1, 'C');
	$pdf -> Cell(12.9, 0.4, 'a lo entregado. El material viaja por cuenta del comprador.', 
		'LR', 0, 'L');
	$pdf -> SetFont('Arial', 'B', 9);
	$pdf -> Cell(2.1, 0.4, '', 'LR', 0, 'C');
	$pdf -> Cell(2.5, 0.4, '', 'LR', 1, 'R');
	$pdf -> Cell(12.9, 0.4, '', 'LBR', 0, 'L');
	$pdf -> Cell(2.1, 0.4, 'Impuesto', 'LBR', 0, 'C');
	$pdf -> SetFont('Arial', '', 9);	
	$pdf -> Cell(2.5, 0.4, $billTotal * ($billTax / 100), 'BR', 1, 'C');	
	$pdf -> SetFont('Arial', '', 9);
	$pdf -> Cell(12.9, 0.4, '', 'LR', 0, 'C');
	$pdf -> Cell(2.1, 0.4, '', 'LR', 0, 'C');
	$pdf -> Cell(2.5, 0.4, '', 'LR', 1, 'R');
	$pdf -> Cell(12.9, 0.4, 
		'        Por Industrias Exhele, C. A.                 Conforme Cliente', 'LBR', 0, 'L');
	$pdf -> SetFont('Arial', 'B', 9);
	$pdf -> Cell(2.1, 0.4, 'Total Bs', 'LBR', 0, 'C');
	$pdf -> SetFont('Arial', '', 9);
	$pdf -> Cell(2.5, 0.4, $billTotal * (1 + $billTax / 100), 'LBR', 1, 'C');
	$pdf -> Output();
?> 